<template>
  <div>
    <el-button type="primary" @click="addPicture()">添加学生信息(上传图片)</el-button>
    <!-- 弹出框 -->
    <el-dialog title="增加学生信息" :visible.sync="dialogFormVisible">
      <el-form :model="student">
        <el-upload
          action="http://localhost:8088/api/upload"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :on-success="uploadOk"
        >
          <i class="el-icon-plus"></i>
          <el-dialog :visible.sync="dialogVisible" size="tiny">
            <img width="100%" :src="dialogImageUrl" alt />
          </el-dialog>
        </el-upload>
        <el-form-item label="学生姓名" :label-width="formLabelWidth">
          <el-input v-model="student.studentname" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="学院" :label-width="formLabelWidth">
          <el-input v-model="student.institute" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="专业" :label-width="formLabelWidth">
          <el-input v-model="student.major" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年级" :label-width="formLabelWidth">
          <el-input v-model="student.grade" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="班级" :label-width="formLabelWidth">
          <el-input v-model="student.clazz" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-input v-model="student.sex" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" :label-width="formLabelWidth">
          <el-input v-model="student.tel" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="student.pwd" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addPictureStudent()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      student: {
        studentname: "",
        institute:"",
        major:"",
        grade:"",
        clazz:"",
        sex:"",
        tel: "",
        pwd:"",
        image: "",
      },
    };
  },
  methods: {
    uploadOk(response, file, fileList) {
      this.student.image = response.data;
    },
    addPicture: function () {
      this.dialogFormVisible = true;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    addPictureStudent: function () {
      this.$axios
        .post('/api/admin/addStudents', {
          'studentname': this.student.studentname,
          'institute': this.student.institute,
          'major': this.student.major,
          'grade': this.student.grade,
          'clazz': this.student.clazz,
          'sex': this.student.sex,
          'tel': this.student.tel,
          'pwd': this.student.pwd,
          'image': this.student.image,
        })
        .then((response) => {
          if (response.data.code == 200) {
            this.$message("添加成功");
            this.dialogFormVisible = false;
            this.$router.push({path:'/adminstudentManage'})
          }
        })
        .catch((error) => {
          this.$message(error);
        });
    },
  },
};
</script>
<style>
</style>